﻿using System.Collections.Generic;
using UnityEngine;
using System;

namespace Model
{
  [Serializable]
  public class ReferenceData
  {
    public string name;
    public UnityEngine.Object Object;
    public bool deleteField = false;
  }
  /// <summary>
  /// 引用管理器
  /// </summary>
  public class ReferenceManage : MonoBehaviour
  {
    private string HorUpdateScriptnNamespace = Defines.HotfixScriptnNamespace;

    public bool disenble = false;


    /// <summary>
    /// 需要同步的热更新脚本
    /// </summary>
    public string ButtJoinHorUpdateScript;

    public string path = System.IO.Directory.GetCurrentDirectory() + @"\Hotfix\Temporary";

    public string tab = "@";

    public GameObject thisObject;

    /// <summary>
    /// 引用名称
    /// </summary>
    public List<ReferenceData> objectList = new List<ReferenceData>();

    /// <summary>
    /// 引用字典
    /// </summary>
    public Dictionary<string, UnityEngine.Object> dicRefManage = new Dictionary<string, UnityEngine.Object>();

    public Dictionary<string, UnityEngine.Object> testDir = new Dictionary<string, UnityEngine.Object>();

    /// <summary>
    /// 是否允许发送脚本实例消息
    /// </summary>
    public bool switcher = true;


    private void Awake()
    {
      AddReference();
      HorUpdateScriptnNamespace = Defines.HotfixScriptnNamespace;
    }

    private void Start()
    {
    }

    private void OnValidate()
    {
      thisObject = gameObject;
    }

    private void AddReference()
    {
      switcher = false;
      dicRefManage.Clear();
      if (!string.IsNullOrEmpty(ButtJoinHorUpdateScript))
      {
        dicRefManage.Add(HorUpdateScriptnNamespace + "." + ButtJoinHorUpdateScript, this.gameObject);
        switcher = true;
      }
      dicRefManage.Add("null", this.gameObject);
      for (int i = 0; i < objectList.Count; i++)
      {
        if (objectList[i].Object == null)
          continue;
        try
        {
          dicRefManage.Add(objectList[i].name, objectList[i].Object);
        }
        catch (Exception e)
        {
          Debug.LogError(e + "引用保存失败,您的引用中有相同的引用名称, 请修改引用");
          return;
        }

      }
    }

  }
}

